<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/10/28
 * Time: 20:05
 */

namespace app\common\model;

use think\model;
use think\db;
use think\facade\Cache;
use think\facade\Config;

class Auth extends model
{
    protected $name = 'auth';
    protected $cachekey = 'allauth';

    protected static $instance;
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($options);
        }
        return self::$instance;
    }

    public function getAllAuths(){
        if(!Cache::get($this->cachekey) || true)
        {
            $list = Db::name('auth')->order('sort desc')->select();
            $authlist = array();
            foreach($list as $k=>$v){
                $authlist[$v['guid']] = $v;
            }
            Cache::set($this->cachekey,$authlist);
        }
        return Cache::get($this->cachekey);
    }

    public function getUserAuthList($uid){
        if(in_array($uid,Config::get('web.superuser')))
            return $this->getAllAuths();
        $allauth = self::getAllAuths();
        $glist = Db::name('group')->alias('g')->leftJoin('user_group u','g.guid=u.gid')->where('u.uid','=',$uid)->select();
        $authlist = '';
        foreach($glist as $v){
            if(!empty($v['auths']))
                $authlist .= $v['auths'].',';
        }
        $usermodel = Db::name('user')->where(['id'=>$uid])->findOrEmpty();
        $authlist .= $usermodel['auths'];
        $authlist = explode(',',$authlist);
        $authlist = array_unique($authlist);

        $auths = array();
        foreach($allauth as $v){
            if(in_array($v['guid'],$authlist))
                $auths[$v['guid']] = $v;
        }
        return $auths;
    }

    public function setUserAuth($uid,$authlist){
        if(in_array($uid,Config::get('web.superuser')))
            return ;
        $glist = Db::name('group')->alias('g')->leftJoin('user_group u','g.guid=u.gid')->where('u.uid','=',$uid)->select();
        if(!is_array($authlist))
        {
            $authlist = explode(',',$authlist);
        }
        $groupauthlist = '';
        foreach($glist as $v){
            if(!empty($v['auths']))
                $groupauthlist .= $v['auths'].',';
        }
        $groupauthlist = explode(',',$groupauthlist);
        $userauthlist = array_diff($authlist,$groupauthlist);
        $userauthlist = implode(',',$userauthlist);
        $usermodel = Db::name('user')->where(['id'=>$uid])->update(['auths'=>$userauthlist]);
        return;
    }

    public function getTreeMenu($authlist,$pid){
        $tree = array();
        foreach($authlist as $k=>$v)
        {
            if($v['pid'] == $pid && $v['ismenu'])
            {
                $item = $v;
                $item['url'] = url($v['action']);
                $item['icon'] = $v['icon'];
                $item['children'] = $this->getTreeMenu($authlist,$v['guid']);
                $tree[] = $item;
            }
        }
        return $tree;
    }

    public function getAllChildren($pid,$deepth=10){
        $list = array();
        if($deepth<=0)
            return array();
        foreach(self::getAllAuths() as $k=>$v){
            if($v['pid'] == $pid){
                $list[] = $v;
                $children = self::getAllChildren($v['guid'],$deepth-1);
                foreach($children as $vv){
                    $list[] = $vv;
                }
            }
        }
        return $list;
    }

    public function getTreeAuth($authlist,$pid){
        $tree = array();
        foreach($authlist as $k=>$v)
        {
            if($v['pid'] == $pid)
            {
                $v['children'] = $this->getTreeAuth($authlist,$v['guid']);
                $tree[] = $v;
            }
        }
        return $tree;
    }

    public function getNewAuthlist($authlist){
        $data = array();
        foreach($authlist as $k=>$v){
            $tmp = array();
            $tmp['id'] = $v['guid'];
            $tmp['pid'] = $v['pid'];
            $tmp['pid'] = empty($tmp['pid'])?'0':$tmp['pid'];
            $tmp['title'] = $v['name'];
            $tmp['icon'] = $v['icon'];
            $tmp['ismenu'] = $v['ismenu'];
            $tmp['action'] = $v['action'];
            $data[] = $tmp;
        }
        return $data;
    }

}